package com.itaem.community.modules.vote.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.vote.entity.VoteEntity;
import com.itaem.community.modules.vote.entity.VoteOptionsEntity;
import com.itaem.community.modules.vote.entity.VoteVo;
import com.itaem.community.modules.vote.service.VoteOptionsService;
import com.itaem.community.modules.vote.service.VoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 投票 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "投票管理")
@ResponseResult
@RestController
@RequestMapping("/vote")
@Slf4j
public class VoteController {

    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteOptionsService optionsService;

    /**
     * 分页查询某一投票数据
     * ！后期需实现同时将投票及选型同时查询！
     * @param page 查询第几页
     * @param size 每页多大
     * @return 返回分页查询信息
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page<VoteEntity> list(@PathVariable Long page, @PathVariable Long size){
        return voteService.page(new Page<>(page, size));
    }

    /**
     * 根据id获取某一投票
     * ！后期需实现同时获取该投票所有选项！
     * @param id 投票表id
     * @return 返回该id投票表及其所有投票选项
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public VoteVo info(@PathVariable Long id){
        if(voteService.getById(id) == null){
            return null;
        }
        VoteVo voteVo = new VoteVo();
        voteVo.setVoteEntity(voteService.getById(id));
        voteVo.setVoteOptionsEntities(
                optionsService.list(new QueryWrapper<VoteOptionsEntity>().eq("vote_id",id)));
        //后期：select * from vote_options where vote_id = id and is_deleted = false;
        return voteVo;
    }

    /**
     * 新增一行投票
     * 已实现同时新增该投票下的投票选项
     * 新增一个投票的时候应该同时把该物业下的所有用户的用户投票表新建出来吧
     * @param voteVo 一个投票及其选项
     * @return 是否完成
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping
    public Boolean save(@RequestBody VoteVo voteVo) {
        VoteEntity voteEntity = voteVo.getVoteEntity();
        List<VoteOptionsEntity> voteOptionsEntities = voteVo.getVoteOptionsEntities();
        voteEntity.setVotedNumber(0);
        voteEntity.setCompleted(false);
        voteEntity.setDeleted(false);
        voteService.save(voteEntity);
        for (VoteOptionsEntity voteOption:voteOptionsEntities) {
            voteOption.setVoteId(voteEntity.getId());
            voteOption.setCount(0);
            voteOption.setDeleted(false);
            optionsService.save(voteOption);
        }
        // voteEntity.setRequiredNumber(
        // select count(*) from user
        // where property_id = voteEntity.property_id
        // and role = normal_user)
        // 所需投票人数计算↑
        return true;
    }

    /**
     * 根据id更新某一投票
     * ！后期需要实现同时修改投票所属各个选项！
     * @param voteVo 一个投票及其选项
     * @return 是否更新完成
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody VoteVo voteVo) {
        try{
            VoteEntity voteEntity = voteVo.getVoteEntity();
            List<VoteOptionsEntity> voteOptionsEntities = voteVo.getVoteOptionsEntities();
            voteService.update(
                    voteEntity, new UpdateWrapper<VoteEntity>().eq("id",voteEntity.getId()));
            for (VoteOptionsEntity voteOption:voteOptionsEntities) {
                voteOption.setVoteId(voteEntity.getId());
                optionsService.update(
                        voteOption, new UpdateWrapper<VoteOptionsEntity>().eq("id",voteOption.getId()));
            }
        } catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 根据投票id逻辑删除某一投票
     * ！后期需同时删除该投票下的投票选项（感觉可以不用，先写着吧）！
     * @param id 投票表id
     * @return 是否成功根据id删除投票表及其下所有投票选项
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"}, logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Boolean remove(@PathVariable Long id) {
        //后期：delete from vote_options where vote_id = id
        return voteService.removeById(id);
    }
}
